package xc;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class deo3 {
    static class my_thread extends Thread {
        static int ticket = 0;
        //共用一把锁，所以要加个static
        static Lock lock = new ReentrantLock();

        public void run(){
                while(true) {
                    lock.lock();
                    try {
                        if (ticket == 100) {
                            break;
                        } else {
                            Thread.sleep(10);
                            ticket++;
                            System.out.println(getName() + "正在卖第" + ticket + "张票.");
                        }
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    } finally {
                        lock.unlock();
                    }
                }
        }
    }

    public static void main(String[] args){
        my_thread t1 = new my_thread();
        my_thread t2 = new my_thread();

        t1.setName("t1");
        t2.setName("t2");

        t1.start();
        t2.start();
    }
}
